/**
 * 
 */
package src;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

import lucene.LuceneConstants;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.LockObtainFailedException;

/**
 * @author Govind Kothari
 *
 */
public class IndexFaq {

	private static IndexWriter iwriter;
	public IndexFaq(String indexDir){
		try{
			Analyzer analyzer = new  WhitespaceAnalyzer();
			iwriter = new IndexWriter(indexDir, analyzer, true);
			iwriter.setMergeFactor(100);
		} catch(Exception e){
			e.printStackTrace();
			System.exit(-1);
		}
	}
	
	public void index(String question, String answer, int docnum){		
		
		try{
			Document document  = new Document();
			document.add(new Field("question", question,Field.Store.YES,Field.Index.TOKENIZED));
			document.add(new Field("answer",answer,Field.Store.YES,Field.Index.UN_TOKENIZED));
			document.add(new Field("docnum",Integer.toString(docnum),Field.Store.YES,Field.Index.UN_TOKENIZED));
			iwriter.addDocument(document);	
			System.out.println("Question  "+question);
			System.out.println("Answer "+answer);
			System.out.println("Docnum "+docnum);
			
		} catch(Exception e){
			e.printStackTrace();
			System.exit(-1);
		}
	}
	

	
	
	public void IndexData(String filename, boolean ANSWER_AVAILABLE){
		System.out.println(filename);
		File infile = new File(filename);
		int docnum =0;
				try {
					BufferedReader reader = new BufferedReader(new InputStreamReader (new FileInputStream (infile), "UTF8"));
					String sentence = null;
					while((sentence = reader.readLine())!=null){
						String question = sentence;
						String answer = null;
						if(ANSWER_AVAILABLE){
							answer = reader.readLine();
						}else{
							answer = "Not Available";
						}
						this.index(question,answer,docnum);
						docnum++;
					}
					reader.close();					
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}		
	}
	
	public static void main(String[] args) {
		IndexFaq ifq = new IndexFaq(LuceneConstants.hindiFAQIndexdir_DanishData);		
		String dir = "C:/DOCUME~1/Administrator/workspace/smsindex/data/Hindi/";
		
		ifq.IndexData(dir+"NOISY-data.hi", false);
		ifq.IndexData(dir+"HINDIFAQ.txt", false);
		
		try {
			iwriter.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//ifq.IndexData(LuceneConstants.hindiFAQ, false);
		//ifq.IndexData(LuceneConstants.telecomfile, true);
		//ifq.IndexData(LuceneConstants.yahoofile, false);		
	}
}